<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if="goodData.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodData.categories[1].id}`">{{goodData.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodData.categories[0].id}`">{{goodData.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem>{{goodData.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <goods-image :img-list="goodData.mainPictures"/>
          <!-- 销售信息 -->
          <goods-sales :good="goodData"/>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <goods-name :good="goodData"/>
          <!-- 商品规格 -->
          <sku :goods="goodData" @change="getSku"/>
          <!-- 数量组件 -->
          <xtx-number :max="5" :min="2" v-model="num"/>
          <!-- 加入购物车按钮 -->
          <xtx-button size="middle" type="primary" @click="addCart" style="margin-top:20px;">加入购物车</xtx-button>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <goods-tabs/>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <good-hot type="1"/>
          <good-hot type="2"/>
          <good-hot type="3"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted, provide } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import Sku from './Sku'
import GoodsTabs from './components/goods-tabs.vue'
import GoodHot from './components/good-hot.vue'
import { useStore } from 'vuex'
import { message } from '@/components/Message/index'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    Sku,
    GoodsTabs,
    GoodHot
  },
  setup () {
    const goodData = ref({})
    provide('good-data', goodData)
    const route = useRoute()
    const num = ref(1)
    async function loadData () {
      const res = await findGoods(route.params.id)
      // console.log(res.result)
      goodData.value = res.result
    }
    onMounted(() => {
      loadData()
    })
    const skuId = ref(null)
    function getSku (sku) {
      console.log(sku)
      if (!sku.skuId) {
        return false
      }
      skuId.value = sku.skuId
    }
    const store = useStore()
    const router = useRouter()
    function addCart () {
      const token = store.state.user.profile.token
      if (!token) {
        router.push({
          path: '/login',
          query: {
            redirect: encodeURIComponent(route.fullPath)
          }
        })
      } else {
        store.dispatch('cart/asyncAddCart', { skuId: skuId.value, count: num.value }).then(() => {
          message({ type: 'success', text: '加入购物车成功' })
        })
      }
    }
    return {
      goodData,
      getSku,
      num,
      addCart
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;
    }
    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
